package com.jinzhending.service.wx.impl;

import com.jinzhending.comm.MoneyConvert;
import com.jinzhending.dao.wx.GoodsListDao;
import com.jinzhending.entity.GoodsInfo;
import com.jinzhending.service.wx.GoodsListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: LieIdle
 * @Package: com.jinzhending.service.wx.impl
 * @Author: huat
 * @Date: 2020/4/6 9:53
 * @Version: 1.0
 */
@Service
public class GoodsListServiceImpl implements GoodsListService {
    @Autowired
    GoodsListDao goodsListDao;
    /**
     * 获取商品列表
     * @param startPage 起始页
     * @param pageSize  页面大小
     * @param cid 分类id
     * @param goodsName 商品名称
     * @return
     */
    @Override
    public Map<String,Object> getGoodList(int startPage, int pageSize, Integer cid, String goodsName) {
        Map<String,Object> map=new HashMap<String,Object>();
        int countGoods=goodsListDao.countGoods(cid, goodsName);
        int countPage=countGoods%pageSize==0?countGoods/pageSize:(countGoods/pageSize)+1;
        if(startPage>countPage){
            startPage=countPage;
            map.put("code",401);
            return map;
        }
        List<GoodsInfo> goodList= goodsListDao.getGoodList((startPage-1)*pageSize, pageSize,cid,goodsName);
        for (GoodsInfo goodsInfo:goodList){
            goodsInfo.setGoodsMoneyNumber(MoneyConvert.fen2YuanStr(goodsInfo.getGoodsMoneyNumber()));
        }

        map.put("data",goodList);
        map.put("countPage",countPage);
        return map;
    }
    /**
     * 获取商品信息通过id
     * @param goodsId 商品id
     * @return
     */
    @Override
    public GoodsInfo getGoodInfoById(Integer goodsId) {
        GoodsInfo goodsInfo=goodsListDao.getGoodInfoById(goodsId);
        if(null!=goodsInfo){
            goodsInfo.setGoodsMoneyNumber(MoneyConvert.fen2YuanStr(goodsInfo.getGoodsMoneyNumber()));
            int result=goodsInfo.getGoodsSeeNumber()+1;
            int resultNumber=goodsListDao.updateGoodsSeeNumberById(goodsInfo.getId(),result);
            if(resultNumber<1){
                goodsInfo=null;
            }
        }


        return goodsInfo;
    }
    /**
     * 通过商品id删除商品
     * @param id
     * @return
     */
    @Override
    public int delGoodInfoById(int id) {

        return goodsListDao.delGoodInfoById(id);
    }
    /**
     * 获取商品通过id
     * @param id
     * @return
     */
    @Override
    public GoodsInfo getGoodById(int id) {
        GoodsInfo goodsInfo=goodsListDao.getGoodById(id);
        goodsInfo.setGoodsMoneyNumber(MoneyConvert.fen2YuanStr(goodsInfo.getGoodsMoneyNumber()));
        return goodsInfo;
    }
}
